﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QLThuVien
{
    public partial class frDanhMuc : Form
    {
        public frDanhMuc()
        {
            InitializeComponent();

        }
        private DataSet ds;
        private SqlDataAdapter da;
        private CurrencyManager bmb;
        public string sketnoi = "";

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            sketnoi = "tacgia";
            ds = new DataSet();
            ds.Clear();
            try
            {
                KetNoi.setconnect();
                da = new SqlDataAdapter("select *from tacgia", KetNoi.scn);
                da.Fill(ds);
                bmb = BindingContext[this.ds.Tables[0]] as CurrencyManager;
                this.dataGridView1.DataSource = ds.Tables[0];
                KetNoi.closeconnect();
            }
            catch { MessageBox.Show("co loi do ket noi", "thong bao"); } 
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            sketnoi = "ngonngu";
            ds = new DataSet();
            ds.Clear();
            try
            {
                KetNoi.setconnect();
                da = new SqlDataAdapter("select *from ngonngu", KetNoi.scn);
                da.Fill(ds);
                bmb = BindingContext[ds.Tables[0]] as CurrencyManager;
                this.dataGridView1.DataSource = ds.Tables[0];
                da.Dispose();
                ds.Dispose();
                KetNoi.closeconnect();
            }
            catch { MessageBox.Show("co loi do ket noi", "thong bao"); } 
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            sketnoi = "loaisach";
            ds = new DataSet();
            ds.Clear();
            try
            {
                KetNoi.setconnect();
                SqlDataAdapter da = new SqlDataAdapter("select *from loaisach", KetNoi.scn);
                da.Fill(ds);
                bmb = BindingContext[this.ds.Tables[0]] as CurrencyManager;
                this.dataGridView1.DataSource = ds.Tables[0];
            }
            catch { MessageBox.Show("co loi do ket noi", "thong bao"); }
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            ds = new DataSet();
            ds.Clear();
            sketnoi = "NXB";
            try
            {
                KetNoi.setconnect();
                da = new SqlDataAdapter("select *from NXB", KetNoi.scn);
                da.Fill(ds);
                bmb = BindingContext[ds.Tables[0]] as CurrencyManager;
                this.dataGridView1.DataSource = ds.Tables[0];
            }
            catch { MessageBox.Show("co loi do ket noi", "thong bao"); }
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            ds = new DataSet();
            ds.Clear();
            sketnoi = "NHACUNGCAP";
            try
            {
                KetNoi.setconnect();
                da = new SqlDataAdapter("select *from NHACUNGCAP", KetNoi.scn);
                da.Fill(ds);
                bmb = BindingContext[ds.Tables[0]] as CurrencyManager;
                this.dataGridView1.DataSource = ds.Tables[0];
            }
            catch { MessageBox.Show("co loi do ket noi", "thong bao"); }
        }

        private void btthem_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet ds1;
                Truyxuatdulieu txdl = new Truyxuatdulieu();
                ds1 = txdl.getdata("select *from " + sketnoi);
                if (btthem.Text == "Chấp Nhận")
                {
                    this.capnhat();
                    btthem.Text = "Thêm";
                    MessageBox.Show("Đã tạo mới thành công", "Thông Báo");
                }
                else
                {
                    ds.Tables[0].Rows.Add();
                    btthem.Text = "Chấp Nhận";
                }
            }
            catch
            { MessageBox.Show("ket noi co loi", "thong bao"); }
        }
        public void capnhat()
        {
            SqlCommandBuilder cb;
            Truyxuatdulieu txdl = new Truyxuatdulieu();
            SqlDataAdapter da = new SqlDataAdapter("select *from " + sketnoi, KetNoi.scn);
            cb = new SqlCommandBuilder(da);
            da.Update(ds);
        }
        private void btsua_Click(object sender, EventArgs e)
        {
            try
            {
                MessageBox.Show("Đã cập nhật thành công", "Thông Báo");
                this.capnhat();
            }
            catch (Exception Ex)
            { MessageBox.Show(Ex.ToString()); }
        }

        private void btxoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Ban that su muon xoa ?", "Thong bao", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                try
                {
                    SqlCommandBuilder cb;
                    Truyxuatdulieu txdl = new Truyxuatdulieu();
                    SqlDataAdapter da = new SqlDataAdapter("select *from " + sketnoi, KetNoi.scn);
                    bmb.RemoveAt(bmb.Position);
                    cb = new SqlCommandBuilder(da);
                    da.Update(ds);
                }
                catch { }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            ds.Clear();
            this.dataGridView1.DataSource = ds;
            this.Hide();
        }



    }
}
